package org.vacoor.nothing.web.service.support.storage.mgt;

import com.jcraft.jsch.*;

import java.util.Vector;

/**
 */
public class JSchDemo {
    public static void main(String[] args) throws JSchException, SftpException {
        JSch jsch = new JSch();
        Session session = jsch.getSession("besture", "192.168.1.123", 22);
        if (null == session) {
            // throw
        }
        session.setPassword("besture");
        // 第一次登陆时候提示, (ask|yes|no)
        session.setConfig("StrictHostKeyChecking", "no");
        session.setConfig("compression.s2c", "zlib,none");
        session.setConfig("compression.c2s", "zlib,none");
        session.connect(3 * 1000);
        session.setTimeout(3 * 1000);

        /*
        ChannelShell shell = (ChannelShell) session.openChannel("shell");

        InputStream inStream = shell.getInputStream();
        OutputStream outStream = shell.getOutputStream();
        Reader reader = new InputStreamReader(inStream, "utf-8");

        String lang = System.getenv().get("LANG");
        shell.setEnv("LANG", lang != null ? lang : "en_US.UTF-8");
        shell.setPtyType("xterm");
        shell.setPtySize(400, 400, 400, 400);
        shell.connect();

        outStream.write(Bytes.toBytes("ls\r\n"));
        outStream.flush();
        System.out.println("write over");

        IOUtils.flow(inStream, System.out, false, false);
        */

        ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
        sftp.connect();
        Vector<ChannelSftp.LsEntry> ls = sftp.ls("/");  // 不存在异常
        for (ChannelSftp.LsEntry l : ls) {
            System.out.println(l);
        }
        sftp.disconnect();

        session.isConnected();
        session.disconnect();

        System.exit(0);
    }
}
